package com.zl.tby.screen.controller;

import com.zl.tby.core.common.R;
import com.zl.tby.dto.TbyEcologyInfoQuery;
import com.zl.tby.model.TbyEcologyInfo;
import com.zl.tby.model.TbyEcologyReal;
import com.zl.tby.screen.service.ITbyEcologyInfoScreenService;
import com.zl.tby.screen.vo.ecology.ChartsData;
import com.zl.tby.screen.vo.ecology.TbyEcologyScreenInfo;
import com.zl.tby.service.ITbyEcologyRealService;
import org.noear.solon.annotation.Controller;
import org.noear.solon.annotation.Inject;
import org.noear.solon.annotation.Mapping;
import org.noear.solon.core.handle.MethodType;
import org.noear.solon.validation.annotation.Valid;

import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * 生态因子大屏数据
 * @author changpeng
 * @version 1.0
 * @since 2024/11/6 13:23
 */
@Valid
@Controller
@Mapping("/ecology_screen")
public class TbyEcologyInfoScreenController {

    @Inject
    private ITbyEcologyInfoScreenService tbyEcologyInfoScreenService;
    @Inject
    private ITbyEcologyRealService tbyEcologyRealService;

    @Mapping(value = "/chart_data", method = MethodType.GET)
    public R<List<ChartsData>> chartData(TbyEcologyInfoQuery query) {
        return R.ok(this.tbyEcologyInfoScreenService.statisticChartData(query));
    }

    /**
     * 生态检测数据图表接口原来只返回平均值，现在增加最高值和最低值2025年10月15日 09:57:15bjs
     * @param query
     * @return
     */
    @Mapping(value = "/chart_data_maxmin", method = MethodType.GET)
    public R<List<ChartsData>> chartDataMaxmin(TbyEcologyInfoQuery query) {
        return R.ok(this.tbyEcologyInfoScreenService.statisticChartDataMaxmin(query));
    }

    @Mapping(value = "/history", method = MethodType.GET)
    public R<?> history(TbyEcologyInfoQuery query) {
        return R.ok(this.tbyEcologyInfoScreenService.historyList(query));
    }

    @Mapping(value = "/detail", method = MethodType.GET)
    public R<TbyEcologyScreenInfo> detail(Long stationId) {
        return R.ok(this.tbyEcologyInfoScreenService.detailByStationId(stationId));
    }

    /**
     * 获取实时天气
     * @param stationId: 监测站id
     * @return 结果，如果没有数据，id = -1
     */
    @Mapping(value = "/get_real_data_by_id", method = MethodType.GET)
    public R<TbyEcologyReal> getRealDataById(Long stationId) {
        TbyEcologyReal entity = this.tbyEcologyRealService.getById(stationId);
        if (entity == null || entity.getId() == null) {
            entity = new TbyEcologyReal();
            entity.setId(-1L);
        } else {
            entity.setCreateTimeFormat(entity.getCreateTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        }
        return R.ok(entity);
    }
}
